import { Form, Input, InputNumber, Table } from 'antd';
import Cls from 'classnames';
import { ToAssetControllerContext, IToAssetControllerActionEnum } from '../mconfig';
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { openMaterielColumns } from '../mconfig';
import { getLoadMapList, getSelectDataMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';

export const UpdateMaterielView: React.FC<any> = ({ openMaterialModalStatus, pageType }) => {
  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`, //资产分类
  ]);
  /** 资产分类 */
  const assetTypeList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`], 'name', 'id', true);

  const { materielList, toAssetControllerDispatch } = useContext(ToAssetControllerContext);

  const delSelectRow = (record) => {
    // 取消选择
    const arr = materielList.filter((item) => {
      return item.stuffId !== record.stuffId || item.id !== record.id;
    });
    toAssetControllerDispatch({
      type: IToAssetControllerActionEnum.CANCEL_MATERIEL,
      payload: arr,
    });
  };

  const changeValue = (value: string | number, id: string | number, col) => {
    let arr = JSON.parse(JSON.stringify(materielList));
    let idx = arr.findIndex((item: any) => {
      return item.stuffId !== item.stuffId || item.id !== item.id;
    });
    if (idx !== -1) {
      arr[idx][col] = value;
    }
    toAssetControllerDispatch({
      type: IToAssetControllerActionEnum.SELECT_MATERIEL,
      payload: arr,
    });
  };

  /** 表格行配置 */
  const columnsWithInput: any = useMemo(() => {
    return openMaterielColumns(openMaterialModalStatus, delSelectRow, assetTypeList, pageType, changeValue);
  }, [materielList]);

  return (
    <div className={Cls('list-table', 'descriptionsType-tableSty')}>
      <Table
        bordered
        size="small"
        dataSource={materielList}
        columns={columnsWithInput}
        rowKey={(res) => (res?.id ? res?.id : res?.stuffId)}
        scroll={{ x: 1200, y: '' }}
        pagination={false}
      />
    </div>
  );
};
